#include <stdafx.h>
#include <gtest/gtest.h>
#include "RunConfiguration.h"


TEST(GTestRunnerTest, ParseMode1) {
    RunConfiguration config;

    ASSERT_FALSE(config.IsListOperation());
    ASSERT_FALSE(config.IsSelfTest());

    int argc = 2;
    _TCHAR* argv[] = {
        _T("IGNOREME"),
        _T("--gtest_list_tests")
    };

    config.Parse(argc, argv);

    ASSERT_TRUE(config.IsListOperation());
    ASSERT_FALSE(config.IsSelfTest());
}

TEST(GTestRunnerTest, ParseMode2) {
    RunConfiguration config;

    int argc = 2;
    _TCHAR* argv[] = {
        _T("IGNOREME"),
        _T("--selftest")
    };

    config.Parse(argc, argv);

    ASSERT_FALSE(config.IsListOperation());
    ASSERT_TRUE(config.IsSelfTest());
}

TEST(GTestRunnerTest, ParseMode3) {
    RunConfiguration config;

    int argc = 3;
    _TCHAR* argv[] = {
        _T("IGNOREME"),
        _T("--gtest_list_tests"),
        _T("--selftest")
    };

    config.Parse(argc, argv);

    ASSERT_TRUE(config.IsListOperation());
    ASSERT_TRUE(config.IsSelfTest());
}

TEST(GTestRunnerTest, ParseFilter1) {
    RunConfiguration config;

    int argc = 1;
    _TCHAR* argv[] = {
        _T("IGNOREME"),
    };

    config.Parse(argc, argv);

    ASSERT_FALSE(config.IsListOperation());
    ASSERT_FALSE(config.IsSelfTest());
    ASSERT_EQ(_T("-GTestRunnerTest.*:PipingListenerTests.*"), config.GetFilter());
}

TEST(GTestRunnerTest, ParseFilter2) {
    RunConfiguration config;

    int argc = 2;
    _TCHAR* argv[] = {
        _T("IGNOREME"),
        _T("--gtest_filter=FOO")
    };

    config.Parse(argc, argv);

    ASSERT_FALSE(config.IsListOperation());
    ASSERT_FALSE(config.IsSelfTest());
    ASSERT_EQ(_T("FOO-GTestRunnerTest.*:PipingListenerTests.*"), config.GetFilter());
}

TEST(GTestRunnerTest, ParseFilter3) {
    RunConfiguration config;

    int argc = 2;
    _TCHAR* argv[] = {
        _T("IGNOREME"),
        _T("--gtest_filter=FOO.*:-BAR.*")
    };

    config.Parse(argc, argv);

    ASSERT_FALSE(config.IsListOperation());
    ASSERT_FALSE(config.IsSelfTest());
    ASSERT_EQ(_T("FOO.*:-BAR.*:GTestRunnerTest.*:PipingListenerTests.*"), config.GetFilter());
}

TEST(GTestRunnerTest, ParsePipeName1) {
    RunConfiguration config;

    int argc = 2;
    _TCHAR* argv[] = {
        _T("IGNOREME"),
        _T("--pipename=FOO")
    };

    config.Parse(argc, argv);

    ASSERT_FALSE(config.IsListOperation());
    ASSERT_FALSE(config.IsSelfTest());
    ASSERT_EQ(_T("\\\\.\\pipe\\FOO"), config.GetPipeName());
}

TEST(GTestRunnerTest, ParseLibPath1) {
    RunConfiguration config;

    int argc = 2;
    _TCHAR* argv[] = {
        _T("IGNOREME"),
        _T("--testlib=FOO")
    };

    config.Parse(argc, argv);

    ASSERT_FALSE(config.IsListOperation());
    ASSERT_FALSE(config.IsSelfTest());
    ASSERT_EQ(_T("FOO"), config.GetTestLib());
}

TEST(GTestRunnerTest, ParseMore) {
    RunConfiguration config;

    int argc = 5;
    _TCHAR* argv[] = {
        _T("IGNOREME"),
        _T("--gtest_filter=FOO"),
        _T("--pipename=BAR"),
        _T("--selftest"),
        _T("--testlib=BAZ")
    };

    config.Parse(argc, argv);

    ASSERT_FALSE(config.IsListOperation());
    ASSERT_TRUE(config.IsSelfTest());
    ASSERT_EQ(_T("FOO"), config.GetFilter());
    ASSERT_EQ(_T("\\\\.\\pipe\\BAR"), config.GetPipeName());
    ASSERT_EQ(_T("BAZ"), config.GetTestLib());
}

TEST(GTestRunnerTest, CreateArgv1) {
    RunConfiguration config;

    int argc = 2;
    _TCHAR* argv[] = {
        _T("IGNOREME"),
        _T("--selftest")
    };

    config.Parse(argc, argv);

    int argc2 = config.GetArgc();
    _TCHAR** argv2 = config.GetArgv();

    ASSERT_EQ(2, argc2);
    ASSERT_EQ(_T("IGNOREME"), tstring(argv2[0]));
    ASSERT_EQ(_T("--selftest"), tstring(argv2[1]));
}

TEST(GTestRunnerTest, CreateArgv2) {
    RunConfiguration config;

    int argc = 3;
    _TCHAR* argv[] = {
        _T("IGNOREME"),
        _T("--gtest_list_tests"),
        _T("--testlib=test_project.dll")
    };

    config.Parse(argc, argv);

    int argc2 = config.GetArgc();
    _TCHAR** argv2 = config.GetArgv();

    ASSERT_EQ(4, argc2);
    ASSERT_EQ(_T("IGNOREME"), tstring(argv2[0]));
    ASSERT_EQ(_T("--gtest_list_tests"), tstring(argv2[1]));
    ASSERT_EQ(_T("--testlib=test_project.dll"), tstring(argv2[2]));
    ASSERT_EQ(_T("--gtest_filter=-GTestRunnerTest.*:PipingListenerTests.*"), tstring(argv2[3]));
}

TEST(GTestRunnerTest, CreateArgv3) {
    RunConfiguration config;

    int argc = 2;
    _TCHAR* argv[] = {
        _T("IGNOREME"),
        _T("--testlib=test_project.dll")
    };

    config.Parse(argc, argv);

    int argc2 = config.GetArgc();
    _TCHAR** argv2 = config.GetArgv();

    ASSERT_EQ(3, argc2);
    ASSERT_EQ(_T("IGNOREME"), tstring(argv2[0]));
    ASSERT_EQ(_T("--testlib=test_project.dll"), tstring(argv2[1]));
    ASSERT_EQ(_T("--gtest_filter=-GTestRunnerTest.*:PipingListenerTests.*"), tstring(argv2[2]));
}

TEST(GTestRunnerTest, CreateArgv4) {
    RunConfiguration config;

    int argc = 3;
    _TCHAR* argv[] = {
        _T("IGNOREME"),
        _T("--gtest_filter=FOO"),
        _T("--testlib=test_project.dll")
    };

    config.Parse(argc, argv);

    int argc2 = config.GetArgc();
    _TCHAR** argv2 = config.GetArgv();

    ASSERT_EQ(3, argc2);
    ASSERT_EQ(_T("IGNOREME"), tstring(argv2[0]));
    ASSERT_EQ(_T("--gtest_filter=FOO-GTestRunnerTest.*:PipingListenerTests.*"), tstring(argv2[1]));
    ASSERT_EQ(_T("--testlib=test_project.dll"), tstring(argv2[2]));
}
